/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TCol.h"
#include "frm_control_TTable.h"

using namespace frm;
using namespace control;

// TCol ///////////////////////////////////////////////////////////////////////

//
void TCol::TStyle::SetFromParent(frm::TStyle *Parent_)
{
  if (!Parent_) {
    return;
  }

  TStyle *Parent = dynamic_cast<TStyle*>(Parent_);
  
  Normal.Filler.SetFromParent(Parent->Normal.Filler);
  Normal.Cursor.SetFromParent(Parent->Normal.Cursor);
  Normal.Border.SetFromParent(Parent->Normal.Border);

  Selected.Filler.SetFromParent(Parent->Selected.Filler);
  Selected.Cursor.SetFromParent(Parent->Selected.Cursor);
  Selected.Border.SetFromParent(Parent->Selected.Border);

  Focused.Filler.SetFromParent(Parent->Focused.Filler);
  Focused.Cursor.SetFromParent(Parent->Focused.Cursor);
  Focused.Border.SetFromParent(Parent->Focused.Border);
}

//
svl::TInfoParam TCol::TStyle::AddInfoTo(svl::TInfoParam Info)
{
  return frm::TStyle::AddInfoTo(Info)
   .Add("Normal"   ,Normal)
   .Add("Selected",Selected)
   .Add("Focused" ,Focused)
  ;
}

//
TCol::TCol(TWindow &Window) : TPanel(Window)
{
  struct TStyle : public TCol::TStyle
  {
    TStyle()
    {
      //Style.Focused.Filler.Set(filler::TFrameRect(192,192,0));

      Style.Focused.Filler.Set(filler::TArray()
        .Add(filler::TColor    (250,250,250))
        .Add(filler::TFrameRect(192,192,0))
      );
    }
  };
  static TStyle Style;

  SetStyle(Style)
   .Width_Fixed(100).Height_ChildrenMax()
   .DirectChildren_Horz().WAlignChildren_Begin().HAlignChildren_Center();
}

//
TCol::~TCol()
{
}

//
TCol &TCol::SetStyle(TStyle &V)
{
  style = &V; 
  TPanel::SetStyle(V.Normal); 
  return *this;
}

//
void TCol::TComponent_Mouse(TMouse Mouse)
{
  switch (Mouse.State) {
    case grf::TMouse::EState_LButtonDown: {
      if (
        TComponent_FocusEnable() &&
        static_cast<TPanel*>(Mouse.TopComponent)->TComponent_TransparentForFocus()
      ){
        Focus_EnterByPos(0); return;
      }
    }  
    break;
  }
  TPanel::TComponent_Mouse(Mouse); 
}

//
void TCol::TComponent_SelectEnter()
{
  TPanel::TComponent_SelectEnter();

  if (!Focus_IsEnter()) { 
    TPanel::UpdateStyle(style->Selected);
  }
}

//
void TCol::TComponent_SelectExit()
{
  TPanel::TComponent_SelectExit();
  TPanel::UpdateStyle(style->Normal);
}

//
void TCol::TComponent_FocusEnter()
{
  TPanel::TComponent_FocusEnter();

  TPanelSelect *Select = GetSelectThisOrParent();

  if (Select) { 
    Select->SetEnter(this);
  }
  TPanel::UpdateStyle(style->Focused);
}

//
void TCol::TComponent_FocusExit()
{
  TPanel::TComponent_FocusExit();

  TPanelSelect *Select = GetSelectThisOrParent();

  if (Select && Select->IsEntered(this)) { 
    TPanel::UpdateStyle(style->Selected);
  }
  else { 
    TPanel::UpdateStyle(style->Normal);
  }
}

//
void TCol::TComponent_SetCaretByX(TGeometryRect &GRect,int XClient)
{
  grf::TCaret &Caret = GetFocus().Caret;

  Caret.Destroy();
  Focus_SetPos(0);
}

//
void TCol::TComponent_SetCaretByPos(TGeometryRect &GRect,int Pos)
{
  grf::TCaret &Caret = GetFocus().Caret;

  Caret.Destroy();
  Focus_SetPos(0);
}

//
void TCol::TComponent_ScrollToVisible(TGeometryRect &GRect)
{
  TFrame *CurrFrame = dynamic_cast<TFrame*>(SearchParent(TFrame::TType())); 
  
  if (CurrFrame) {
    //TGeometryRect GRectClient; GetForm().CalcGeometryRectExt
    //  (CurrFrame->Client,GRectClient);
    grf::TRect R; 
    
    if (GRect.RThisVisible.Width <= 0) {
      R.Left   = GRect.RThis.Left;
      R.Top    = GRect.RThis.Top;
      R.Width  = GRect.RThis.Width;//GRectClient.RThisVisible.Width;
      R.Height = GRect.RThis.Height;
    }
    else {
      R.Left   = GRect.RThisVisible.Left;
      R.Top    = GRect.RThis.Top;
      R.Width  = GRect.RThisVisible.Width;//GRectClient.RThisVisible.Width;
      R.Height = GRect.RThis.Height;
    }
    CurrFrame->ScrollToVisibleRect(*this,R);
  }
}

//
void TCol::TComponent_AddToParent(TComponent *Parent)
{
  TPanel::TComponent_AddToParent(Parent);

  TTable *Table = dynamic_cast<TTable*>(SearchParent(TTable::TType())); 
  if (Table) {
    Table->LinkColToGroup(this,Parent->GetNChild(this));
  }



/****************
  if (!dynamic_cast<TRowCaption*>(Parent)) { 
    Except("TComponent_AddToParent[!dynamic_cast<TRowCaption*>(Parent)]"); 
  }

  struct TStyle : public TSplitterVert::TStyle
  {
    TStyle()
    {
      Size = 9;
      Normal.Filler.Set(filler::TLineVertCenter(192,192,192));
      Down  .Filler.Set(filler::TLineVertCenter(192,0,0));
    }
  };
  struct TStyleLast : public TSplitterVert::TStyle
  {
    TStyleLast()
    {
      Size = 5;
      Normal.Filler.Set(filler::TLineVertRight(192,192,192));
      Down  .Filler.Set(filler::TLineVertRight(192,0,0));
    }
  };
  static TStyle     Style;
  static TStyleLast StyleLast;

  TSplitterVert *Splitter = new TSplitterVert(static_cast<TPanel*>(Parent)->GetWindow(),this);
  Splitter->SetMinSize(0);
  Splitter->SetStyle(StyleLast);
  Parent->AddChildAfter(Splitter,this,true);
  
  //
  TSplitterVert *PrevSplitter = dynamic_cast<TSplitterVert*>(
    static_cast<TPanel*>(Parent)->SearchPrevChild(TSplitterVert::TType(),Splitter,false)
  );
  if (PrevSplitter) { 
    PrevSplitter->SetStyle(Style); 
  }
    
  TTable *Table = dynamic_cast<TTable*>(SearchParent(TTable::TType())); if (Table) {
    Table->LinkColToGroup(this,Parent->GetNChild(this));
  }
******************************/  
}

//
void TCol::TComponent_BeforeDeleteRow(TTable *Table)
{
  Table->UnlinkCol(this);
}

//
void TCol::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
}
